package com.heer.gymnasium.module.main.views;

import android.text.TextUtils;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.heer.gymnasium.R;
import com.heer.gymnasium.api.ResponseData.ApiResponse;
import com.heer.gymnasium.api.SignHelper;
import com.heer.gymnasium.api.callback.BaseCallback;
import com.heer.gymnasium.base.BaseFragment;
import com.heer.gymnasium.constant.Type;
import com.heer.gymnasium.module.coach.views.CoachAuthorityActivity;
import com.heer.gymnasium.module.hobby.views.HobbyActivity;
import com.heer.gymnasium.module.login.views.LoginActivity;
import com.heer.gymnasium.module.message.views.MessageListActivity;
import com.heer.gymnasium.module.my.beans.UserBean;
import com.heer.gymnasium.module.my.views.FaceActivity;
import com.heer.gymnasium.module.card.views.MyCardActivity;
import com.heer.gymnasium.module.my.views.MyCollectActivity;
import com.heer.gymnasium.module.my.views.MyInfoActivity;
import com.heer.gymnasium.module.order.views.MyOrderListNewActivity;
import com.heer.gymnasium.module.team.views.MyTeamListActivity;
import com.heer.gymnasium.util.DialogUtils;
import com.heer.gymnasium.util.ImageUtil;
import com.heer.gymnasium.util.Session;
import com.tencent.bugly.crashreport.biz.UserInfoBean;
import com.zhihuiyun.library.dialog.CommonDialog;
import com.zhihuiyun.library.view.CircleImageView;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by yan on 2018/1/11.
 * 我的
 */

public class MyFragment extends BaseFragment {
    private static final int HEAR_ID = R.id.layout_me_head_civ;
    private static final int USERNAME_ID = R.id.layout_me_name_tv;
    private static final int ROLE_ID = R.id.layout_me_role_tv;
    private static final int IPHONE_ID = R.id.layout_me_iphone_tv;
    private static final int CARD_ID = R.id.layout_me_card_rl;
    private static final int ORDER_ID = R.id.layout_me_order_rl;
    private static final int COLLECT_ID = R.id.layout_me_collect_rl;
    private static final int FACE_ID = R.id.layout_me_face_rl;
    private static final int COACH_ID = R.id.layout_me_coach_rl;
    private static final int RES_ID_HOBBY = R.id.layout_me_hobby_rl;
    private static final int RES_ID_TEAM = R.id.layout_me_team_rl;
    private static final int RES_ID_MSG = R.id.layout_me_message_rl;
    private static final int RES_ID_LOGOUT = R.id.layout_me_logout_tv;

    @BindView(HEAR_ID)
    CircleImageView circleImageView;
    @BindView(IPHONE_ID)
    TextView enterTextView;
    @BindView(USERNAME_ID)
    TextView usernameTextView;
    @BindView(ROLE_ID)
    TextView roleTextView;
    @BindView(CARD_ID)
    RelativeLayout cardRelativeLayout;
    @BindView(ORDER_ID)
    RelativeLayout orderRelativeLayout;
    @BindView(COLLECT_ID)
    RelativeLayout collectRelativeLayout;
    @BindView(FACE_ID)
    RelativeLayout faceRelativeLayout;
    @BindView(COACH_ID)
    RelativeLayout coachRelativeLayout;


    @Override
    protected int setConvertView() {
        return R.layout.layout_me;
    }

    @Override
    protected void init() {
        super.init();
    }

    @Override
    protected void initView() {
        super.initView();
    }

    @Override
    protected void loadData() {
        super.loadData();

    }

    @Override
    public void onResume() {
        super.onResume();
        getUserinfo();
    }

    @OnClick(IPHONE_ID)
    public void myInfoOnClick(View view) {
        MyInfoActivity.startActivity(this.getActivity());
    }

    @OnClick(RES_ID_MSG)
    public void myMsgOnClick(View view) {
        MessageListActivity.startActivity(this.getActivity());
    }

    @OnClick(CARD_ID)
    public void myCardOnClick(View view) {
        MyCardActivity.startActivity(this.getActivity());
    }

    @OnClick(ORDER_ID)
    public void myOrderOnClick(View view) {
        MyOrderListNewActivity.startActivity(this.getActivity(), 1);
    }

    @OnClick(COLLECT_ID)
    public void myCollectOnClick(View view) {
        MyCollectActivity.startActivity(this.getActivity());
    }

    @OnClick(FACE_ID)
    public void faceOnClick(View view) {
        FaceActivity.startActivity(this.getActivity());
    }

    @OnClick(RES_ID_HOBBY)
    public void hobbyOnClick(View view) {
        HobbyActivity.startActivity(this.getActivity());
    }

    @OnClick(RES_ID_TEAM)
    public void teamOnClick(View view) {
        MyTeamListActivity.startActivity(this.getActivity());
    }

    @OnClick(COACH_ID)
    public void coachOnClick(View view) {
        CoachAuthorityActivity.startActivity(this.getActivity());
    }

    @OnClick(RES_ID_LOGOUT)
    public void doLogout(View view) {
        DialogUtils.showDialog(getActivity(), "", "确认退出当前账号?", "", "", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogUtils.dismissDialog();
                if (v.getId() == CommonDialog.RES_ID_POSITIVE) {
                    LoginActivity.logout(getContext());
                }
            }
        });
    }

    /**
     * 获取用户信息
     */
    public void getUserinfo() {
        SignHelper signHelper = new SignHelper("");
        mAppHelper.apiService().getuserInfo(signHelper.getToken()).enqueue(new BaseCallback<ApiResponse<UserBean>>() {
            @Override
            public void onSuccess(Call<ApiResponse<UserBean>> call, Response<ApiResponse<UserBean>> response) {
                if (response.isSuccessful() && response.body().isSccessful()) {
                    UserBean bean = response.body().getData();
                    if (bean != null) {
                        if (TextUtils.isEmpty(bean.getNickname())) {
                            usernameTextView.setText("您还未设置用户名");
                        } else {
                            usernameTextView.setText(bean.getNickname());
                        }
                        if (TextUtils.equals(Type.NO, bean.getPhone())) {
                            enterTextView.setText("您还未绑定手机号");
                        } else {
                            String iphone = bean.getPhone();
                            iphone = iphone.replace(iphone.substring(3, 7), "****");
                            enterTextView.setText(iphone);

                        }

                        if (TextUtils.equals(bean.getType(), Type.YES)) {
                            roleTextView.setText("#教练");
                            coachRelativeLayout.setVisibility(View.VISIBLE);
                        } else {
                            roleTextView.setText("#会员");
                            coachRelativeLayout.setVisibility(View.GONE);
                        }

                        ImageUtil.loadUrl(getContext(), bean.getAvatar(), R.drawable.ic_default_head, circleImageView);

                        Session.setUserId(bean.getId());
                        Session.setUserPicUrl(bean.getAvatar());
                    }
                }
            }

            @Override
            public void onFailure(Call<ApiResponse<UserBean>> call, Throwable t) {
                super.onFailure(call, t);
            }
        });

    }
}
